package com.atguigu.gmall.product.utils;

import org.csource.fastdfs.*;
import org.springframework.util.ClassUtils;

/**
 * @author: 李旭
 * @create: 2021-07-06 09:15
 *
 *  管理文件
 *     上传
 *     下载
 *     删除
 **/
public class FastDFSUtils {

    private static TrackerClient trackerClient = null;

    static {
        try {
            String path = ClassUtils.class.getClassLoader().getResource("fdfs_client.conf").getPath();
            System.out.println("path:" + path);
            //0:加载客户端的配置文件  IO流 只认绝对路径 不认相对路径
            ClientGlobal.init(path);
            //1:连接Tracker服务器
            trackerClient = new TrackerClient();

        } catch (Exception e){
            e.printStackTrace();
        }
    }

    //上传文件
    public static String upload(byte[] file,String ext){

        String file_id = null;
        try {
            //2:连接Tracker
            TrackerServer trackerServer = trackerClient.getConnection();
            //3:升级版的StorageClient1
            StorageServer storageServer = null;//扩展
            StorageClient1 storageClient1 = new StorageClient1(trackerServer,storageServer);
            //4:上传文件
            file_id = storageClient1.upload_file1(file, ext, null);
        } catch (Exception e) {
        }
        return file_id;
    }
    //下载文件
    public static byte[] download(String fileId){
        byte[] bytes = null;
        try {
            //2:连接Tracker
            TrackerServer trackerServer = trackerClient.getConnection();
            //3:升级版的StorageClient1
            StorageServer storageServer = null;//扩展
            StorageClient1 storageClient1 = new StorageClient1(trackerServer,storageServer);
            //4:上传文件
            bytes = storageClient1.download_file1(fileId);
        } catch (Exception e) {
        }
        return bytes;
    }
    //删除文件

}
